package net.zdreamer.geracomium.ui.home;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.geracomium.R;

import net.zdreamer.geracomium.myviews.CustomProgressDialog;
import net.zdreamer.geracomium.ui.BaseActivity;
import net.zdreamer.geracomium.utils.httputils.AssessService;
import net.zdreamer.geracomium.utils.tools_utils.ConnectityUtils;
import net.zdreamer.geracomium.utils.tools_utils.URLTools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AssessActivity extends BaseActivity implements OnClickListener {

    private TextView assess1, assess2, title, aiyaaiya1, aiyaaiya2;
    private ImageView img_assess1, img_assess2, back;
    private String table0, table1;
    private Dialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_assess;
    }

    @Override
    protected void init() {
        aiyaaiya1 = (TextView) findViewById(R.id.aiyaaiya1);
        aiyaaiya2 = (TextView) findViewById(R.id.aiyaaiya2);
        assess1 = (TextView) findViewById(R.id.textview_assess1);
        assess2 = (TextView) findViewById(R.id.textview_assess2);
        title = (TextView) findViewById(R.id.title_content);
        img_assess1 = (ImageView) findViewById(R.id.imageview_assess1);
        img_assess2 = (ImageView) findViewById(R.id.imageview_assess2);
        back = (ImageView) findViewById(R.id.back);

        loadingDialog=CustomProgressDialog.createLoadingDialog(AssessActivity.this, "获取评估表……");
    }

    @Override
    protected void initViews() {
        /*
         * 发送请求，获得级别数据
		 */
        if (ConnectityUtils.isNetConnected(this)) {
            AssessService.AssessRequest(URLTools.AssessUrl, handler);
            loadingDialog.show();
        } else {
            Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show();
//            img_assess2.setClickable(false);
//            img_assess1.setClickable(false);
        }
        /*
         * assess1.setText(text);assess2.setText(text);
		 */
    }

    @Override
    protected void initListener() {
        aiyaaiya1.setOnClickListener(this);
        aiyaaiya2.setOnClickListener(this);
        img_assess1.setOnClickListener(this);
        img_assess2.setOnClickListener(this);
        assess2.setOnClickListener(this);
        assess1.setOnClickListener(this);
        title.setText("在线评估");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(back)) {
            this.finish();
        } else if (!aiyaaiya1.getText().toString().equals("") && !aiyaaiya2.getText().toString().equals("")) {


            if ((v.equals(img_assess1)) || (v.equals(img_assess2))) {
                Intent intent = new Intent(this, ResultOfAssessActivity.class);
                intent.putExtra("which", v.equals(img_assess1) ? 1 : 2);
                intent.putExtra("table0", table0);
                // System.out.println("dsfaf"+table0);
                startActivity(intent);
            } else {
                Intent intent = new Intent(this, DoAssessActivity.class);
                intent.putExtra("which",
                        ((v.equals(assess1)) || (v.equals(aiyaaiya1))) ? 1 : 2);
                intent.putExtra("table",
                        ((v.equals(assess1)) || (v.equals(aiyaaiya1))) ? table0 : table1);
                startActivity(intent);
            }
        } else {
            Toast.makeText(this, "链接失败", Toast.LENGTH_SHORT).show();
        }
    }

    private Handler handler = new Handler() {
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.arg1) {
                case 0:
                    loadingDialog.dismiss();
                    // System.out.println(" CustomProgressDialog.createLoadingDialog(AssessActivity.this,\"获取评估表……\").dismiss();");
                    try {
                        JSONArray jsonArray = new JSONArray(msg.obj.toString());
                        JSONObject jsonObject = new JSONObject(jsonArray.getString(1));
                        JSONObject jsonObject1 = new JSONObject(jsonArray.getString(3));
                        aiyaaiya1.setText(jsonObject.getString("title"));
                        aiyaaiya2.setText(jsonObject1.getString("title"));

                        table0 = jsonObject.getString("questions");
                        table1 = jsonObject1.getString("questions");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                default:
                    break;
            }

            // System.out.println(msg.objg);
        }
    };
}
